ROOT := ../../../

CC := /opt/cell/toolchain/bin/spu-gcc
CPP := /opt/cell/toolchain/bin/spu-g++


TARGET_INSTALL_DIR := $(ROOT)/build/tasks

CCACHE := /usr/bin/ccache

#COMMONFLAGS := -DTASKDEBUG

CC_OPT :=  -O3
CLAGS := -W -Wall -Winline -Wno-main $(COMMONFLAGS)
CPPFLAGS := -W -Wall -Winline $(COMMONFLAGS)
#CFLAGS := -mwarn-reloc -merror-reloc $(COMMONFLAGS)

INCLUDES :=	-I. \
			-I$(ROOT)/include/spu \
			-I$(ROOT)/include/shared \
			-I/opt/cell/sysroot/usr/spu/include \
			-I/opt/cell/sysroot/opt/cell/sdk/usr/spu/include

LD_GEN := 	$(IMPORTS) -Wl,-N -Wl,-gc-sections \
			-Wl,--entry,csp_entry -Wl,-u,csp_entry \
			-Wl,-N $(ROOT)/build/lib/spu/libcsp_spu.a \
			-L/opt/cell/sysroot/opt/cell/sdk/usr/spu/lib 


#LDFLAGS0 :=	-Wl,--section-start,.init=0x010000 \
#			-Wl,--defsym=__stack=0x1fff0
#LDFLAGS1 := -Wl,--section-start,.init=0x020000 \
#			-Wl,--defsym=__stack=0x2fff0
#LDFLAGS2 := -Wl,--section-start,.init=0x030000 \
#			-Wl,--defsym=__stack=0x3fff0


#LDFLAGS0 :=	-Wl,--section-start,.init=0x10000 \
#			-Wl,--defsym=__stack=0x27ff0
#LDFLAGS1 := -Wl,--section-start,.init=0x28000 \
#			-Wl,--defsym=__stack=0x3fff0

# 115K to each program
LDFLAGS0 :=	-Wl,--section-start,.init=0x6400 \
			-Wl,--defsym=__stack=0x23000
LDFLAGS1 := -Wl,--section-start,.init=0x233F0 \
			-Wl,--defsym=__stack=0x3fff0






CFILES = ${wildcard *.c}
CPPFILES = ${wildcard *.cpp}
OBJECT_FILES = $(CFILES:.c=.o) $(CPPFILES:.cpp=.o)

all: exit csp_program install

csp_program: $(OBJECT_FILES)
	$(CCACHE) $(CC) -o $(PROGRAM_csp)_0 $(OBJECT_FILES) $(LD_GEN) $(LDFLAGS0)
	$(CCACHE) $(CC) -o $(PROGRAM_csp)_1 $(OBJECT_FILES) $(LD_GEN) $(LDFLAGS1)
#	$(CCACHE) $(CC) -o $(PROGRAM_csp)_2 $(OBJECT_FILES) $(LD_GEN) $(LDFLAGS2)

%.o: %.c
	$(CCACHE) $(CC) $(CC_OPT) $(CFLAGS) $(INCLUDES) -c $+

%.o: %.cpp
	$(CCACHE) $(CPP) $(CC_OPT) $(CPPFLAGS) $(INCLUDES) -c $+


clean:
	rm -fr *.d *.o  $(PROGRAM_csp)_* $(_INSTALL_FILES)



#install: $(_INSTALL_FILES)

MKDIR = mkdir -p
CP = cp -pf

ifeq "$(PROGRAM_csp)" ""
exit:
	@echo "PROGRAM_csp must be set."
	@exit 1
else
exit:
endif

ifneq "$(TARGET_INSTALL_DIR)" ""
install:$(_INSTALL_FILES)
	$(MKDIR) $(TARGET_INSTALL_DIR)
	$(CP) $(PROGRAM_csp)_* $(TARGET_INSTALL_DIR)
else
install:
endif

